﻿using System;
using System.IO;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using MyCompanyName.AbpZeroTemplate.EntityFrameworkCore;
using MyCompanyName.AbpZeroTemplate.Web.Helpers;

namespace MyCompanyName.AbpZeroTemplate.Web.Startup
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CurrentDirectoryHelpers.SetCurrentDirectory();
            //var builder = CreateWebHostBuilder(args);
            //builder.Build().Run();
            var webHost = CreateWebHost(args);
            webHost.Run();
        }

        public static IConfiguration GetConfiguration(string[] args)
        {
            var config = new ConfigurationBuilder()
              .SetBasePath(Directory.GetCurrentDirectory())
              .AddEnvironmentVariables("EX_")
              .AddEnvironmentVariables("ASPNETCORE_")
              .AddCommandLine(args)
              .Build();
            return config;
        }

        public static IWebHost CreateWebHost(string[] args)
        {
            var config = GetConfiguration(args);
            IWebHostBuilder builder = new WebHostBuilder()
                .UseConfiguration(config)
                .UseKestrel(opt =>
                {
                    opt.AddServerHeader = false;
                    opt.Limits.MaxRequestLineSize = 16 * 1024;
                })
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseIIS()
                .UseIISIntegration()
                .UseStartup<Startup>();
            IWebHost webHost = builder.Build();

            return webHost;
        }
    }
}
